//
//  ZHLEEWelcomeController.swift
//  ZHBlog_swift
//
//  Created by ZHLEEBlander on 15/10/14.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit
import SDWebImage

class ZHLEEWelcomeController: UIViewController {

    /// 头像照片
    private lazy var headImageView: UIImageView = {
    
        let imageView = UIImageView()
        
        imageView.size = CGSizeMake(90, 90)
        
        //设置圆角
        imageView.layer.cornerRadius = imageView.height * 0.5
        imageView.layer.masksToBounds = true
        
        //边线
        imageView.layer.borderColor = UIColor.darkGrayColor().CGColor
        imageView.layer.borderWidth = 2
        
        if let url = NSURL(string: (ZHLEEUserAccount.loadAccount()?.avatar_large)!){
            
            imageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        return imageView
    }()
/// 设置显示的"欢迎回来"Label
    private lazy var infoLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "欢迎回来"
        
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16)
        
        label.sizeToFit()
        
        label.hidden = true
        
        return label
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.whiteColor()
        
        view.addSubview(headImageView)
        view.addSubview(infoLabel)
    }
    /**
    重写view将要显示的方法,执行动画
    
    - parameter animated: <#animated description#>
    */
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //执行动画
        headImageView.centerX = ScreenW * 0.5
        headImageView.y = 200
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping:0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.headImageView.y = 100
            
        }) { (finished) -> Void in
           
            self.infoLabel.centerX = self.headImageView.centerX
            self.infoLabel.y = CGRectGetMaxY(self.headImageView.frame) + 5
            
            self.infoLabel.alpha = 0
            
            self.infoLabel.hidden = false
            
            UIView.animateWithDuration(1, animations: { () -> Void in
                self.infoLabel.alpha = 1
                }, completion: { (finished) -> Void in
                    
                    //动画执行结束,跳转首页
                    self.performSelector("toHome", withObject: nil, afterDelay: 1)
            })
                
        }
        
    }
    
    @objc private func toHome(){
    
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        delegate.window?.rootViewController = ZHLEETabBarVC()
    }
}
